package com.atguigu.yygh.cmn.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictEeVo;
import org.springframework.beans.BeanUtils;

public class UploadDataListener implements ReadListener<DictEeVo> {

    private DictMapper dictMapper;

    public UploadDataListener (){

    }

    public UploadDataListener(DictMapper dictMapper){
        this.dictMapper=dictMapper;
    }

    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {

        //调用方法添加数据库
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictEeVo,dict);

        //判断上传的数据id,数据库中是否存在
        Dict dictDB  = dictMapper.selectById(dict.getId());
        if(dictDB==null){
            dictMapper.insert(dict);
        }
       else {
           dictMapper.updateById(dict);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
